﻿
/*
   作者: yqg
   日期: 2011-03-24
 * 描述:数据校验类
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

using System.Globalization;

 
    /// <summary>
    /// 数据校验类,有待补充
    /// </summary>
    public class ValidationHelper
    {
        #region 编号验证

        /// <summary>
        /// 检查指定字符串是否GUID,需补充检验标准
        /// </summary>
        /// <param name="strGuid">待校验的字符串</param>
        /// <returns></returns>
        public static bool IsGuid(string strGuid)
        {
            if (!string.IsNullOrEmpty(strGuid))
            {
                /* 8(-)4(-)4(-)4(-)12|{8-4-4-4-12}|(8-4-4-4-12) */

                // 10244798-9a34-4245-b1ef-9143f9b1e68a
                //Console.WriteLine(guid.ToString("D"));

                // 102447989a344245b1ef9143f9b1e68a
                //Console.WriteLine(guid.ToString("N"));

                // {10244798-9a34-4245-b1ef-9143f9b1e68a}
                //Console.WriteLine(guid.ToString("B"));

                // (10244798-9a34-4245-b1ef-9143f9b1e68a)
                //Console.WriteLine(guid.ToString("P"));

                //@"^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$"
                //@"^[0-9a-f]{8}((-)?[0-9a-f]{4}){3}(-)?[0-9a-f]{12}|\{[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}\}|\([0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}\)$"
                //@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"
                return Regex.IsMatch(strGuid
                    , @"^[0-9a-f]{8}((-)?[0-9a-f]{4}){3}(-)?[0-9a-f]{12}|\{[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}\}|\([0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}\)$", RegexOptions.IgnoreCase);
            }

            return false;
        }

        /// <summary>
        /// 检查指定字符串是否系统编号,需补充检验标准
        /// </summary>
        /// <param name="mangerNumber">待校验的字符串</param>
        /// <returns></returns>
        public static bool IsMangerNumber(string mangerNumber)
        {

            return true;
        }

        #endregion

        #region 时间格式验证

        /// <summary>
        /// 检查指定字符串是否DateTime,需补充检验标准
        /// </summary>
        /// <param name="sDateTime"></param>
        /// <returns></returns>
        public static bool IsDateTime(string sDateTime)
        {
            if (!string.IsNullOrEmpty(sDateTime))
            {
                // ^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$
                // @"^(\d{1,4})(-|\/|\.)(\d{1,2})(-|\/|\.)(\d{1,2})(\s(\d{1,2}):(\d{1,2})(:(\d{1,2})){0,1}){0,1}$"

                /* 
                 * 20130519,闰年的2月份有29天，但对于“2012-02-28 17:37:13”，则验证为false...
                 * 年月日分隔符，暂不支持/，只支持-
                 * 20130622,增加格式支持：yyyy-MM-dd HH:mm
                 */

                //string regexPattern = @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))(\s(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)?$";

                string regexPattern = @"^((((1[6-9]|[2-9]\d)\d{2})[-/.](0?[13578]|1[02])[-/.](0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})[-/.](0?[13456789]|1[012])[-/.](0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})[-/.]0?2[-/.](0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))[-/.]0?2[-/.]29[-/.]))(\s(20|21|22|23|[0-1]?\d):[0-5]?\d(:[0-5]?\d)?)?$";

                return Regex.IsMatch(sDateTime, regexPattern, RegexOptions.IgnoreCase);
            }
            return false;
        }

        #endregion

        #region 数值验证

        /// <summary>
        /// 是否是单精度
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static bool IsFloat(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                return Regex.IsMatch(sValue, @"^[+|-]?\d*\.?\d*$", RegexOptions.IgnoreCase);
            }
            return false;
        }

        /// <summary>
        /// 有理数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNumeric(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                return Regex.IsMatch(sValue, @"^[+-]?\d*[.]?\d*$");
            }
            return false;
        }

        /// <summary>
        /// 整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsInt(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                return Regex.IsMatch(sValue, @"^[+-]?\d*$");
            }
            return false;
        }

        /// <summary>
        /// 小数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsUnsign(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                return Regex.IsMatch(sValue, @"^\d*[.]?\d*$");
            }
            return false;
        }

        /// <summary>
        /// 判断后带位的实数
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static bool IsDonwNumFromOneToFour(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                return Regex.IsMatch(sValue, @"^\-|[0-9]+(.[0-9]{1,4})?$");
            }
            return false;
        }

        #endregion

        #region IP地址+端口验证

        /// <summary>
        /// 判断是否是IP地址格式 0.0.0.0
        /// </summary>
        /// <param name="sValue">待判断的IP地址</param>
        /// <returns>true or false</returns>
        public static bool IsIPAddress(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue) && (sValue.Length >= 7 && sValue.Length <= 15))
            {
                // @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$"
                // @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
                //@"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])$";

                //string regexPattern = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])$";

                string regexPattern = @"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))";

                return Regex.IsMatch(sValue, regexPattern, RegexOptions.IgnoreCase);
            }

            return false;
        }

        /// <summary>
        /// 目标IP地址的存在性检查
        /// </summary>
        /// <param name="pageUrl"></param>
        /// <param name="ipAndPort_HostCheck">主机地址检查，匹配目标http://172.22.104.175:80/。可以考虑使用正则表达式</param>
        /// <param name="bRegexPattern">是否是正则表达式,true-正则;false-非正则</param>
        /// <param name="bError"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static void IpAddress_Exist(string pageUrl, string ipAndPort_HostCheck, bool bRegexPattern, ref bool bError, ref string errorMsg)
        {
            bError = false;
            errorMsg = string.Format("地址的存在性检查。页面地址：{0}; 地址校验:{1}", pageUrl, ipAndPort_HostCheck);

            // 匹配目标http://172.22.104.175:80/，替换结果http://10.35.153.241:80/
            //string regexPattern_Ip = @"[a-zA-z]+://((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))(\:\d{1,5})?/";
            string regexPattern_Ip = @"[a-zA-z]+://(((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))|(localhost))(\:\d{1,5})?/";

            // 1.检查是否存在IP地址
            if (!string.IsNullOrEmpty(pageUrl) && (!string.IsNullOrEmpty(ipAndPort_HostCheck)))
            {
                bool bCheckUrl = Regex.IsMatch(pageUrl, regexPattern_Ip, RegexOptions.IgnoreCase);
                errorMsg = string.Format("{0}; 页面地址验证:{1} ", errorMsg, bCheckUrl);

                bool bCheckHost = false;

                if (bRegexPattern) { bCheckHost = true; }
                else { bCheckHost = Regex.IsMatch(ipAndPort_HostCheck, regexPattern_Ip, RegexOptions.IgnoreCase); }
                errorMsg = string.Format("{0}; 备用验证规则检查:{1} ", errorMsg, bCheckHost);

                if (bCheckUrl && bCheckHost)
                {
                    // 2.将来源地址，替换为目标地址
                    bError = Regex.IsMatch(pageUrl, ipAndPort_HostCheck, RegexOptions.IgnoreCase);

                    if (bError) { errorMsg = "页面地址中，存在指定的主机IP地址"; }
                }
            }
        }

        /// <summary>
        /// IP地址切换，针对文件地址URL做IP地址的存在性验证，并替换IP
        /// </summary>
        /// <param name="fileUrl"></param>
        /// <param name="ipAndPort_Src">匹配目标http://172.22.104.175:80/</param>
        /// <param name="ipAndPort_Dst">替换结果http://10.35.153.241:80/</param>
        /// <param name="bError"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static string IpAddress_Switch(string fileUrl, string ipAndPort_Src, string ipAndPort_Dst, ref bool bError, ref string errorMsg)
        {
            bError = false;
            errorMsg = string.Format("地址切换失败。访问地址：{0}; 来源地址:{1}; 目标地址:{2}", fileUrl, ipAndPort_Src, ipAndPort_Dst);

            // 匹配目标http://172.22.104.175:80/，替换结果http://10.35.153.241:80/
            //string regexPattern_Ip = @"[a-zA-z]+://((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))(\:\d{1,5})?/";
            string regexPattern_Ip = @"[a-zA-z]+://(((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))|(localhost))(\:\d{1,5})?/";

            // 1.检查是否存在IP地址
            if (!string.IsNullOrEmpty(fileUrl) && (!string.IsNullOrEmpty(ipAndPort_Src) && !string.IsNullOrEmpty(ipAndPort_Dst)))
            {
                bool bCheckUrl = Regex.IsMatch(fileUrl, regexPattern_Ip, RegexOptions.IgnoreCase);
                bool bCheckSrc = Regex.IsMatch(ipAndPort_Src, regexPattern_Ip, RegexOptions.IgnoreCase);
                bool bCheckDst = Regex.IsMatch(ipAndPort_Dst, regexPattern_Ip, RegexOptions.IgnoreCase);

                if (bCheckUrl && bCheckSrc && bCheckDst)
                {
                    // 2.将来源地址，替换为目标地址
                    fileUrl = Regex.Replace(fileUrl, ipAndPort_Src, ipAndPort_Dst, RegexOptions.IgnoreCase);

                    bError = false;
                    errorMsg = "地址切换成功";
                }

                //Regex.Match(fileUrl, regexPattern_Ip, RegexOptions.IgnoreCase);
            }

            return fileUrl;
        }

        /// <summary>
        /// 判断是否是IP地址格式 0.0.0.0:1433
        /// </summary>
        /// <param name="sValue">待判断的IP地址,可带端口</param>
        /// <returns>true or false</returns>
        public static bool IsIPAddress_WithPort(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue) && (sValue.Length >= 7 && sValue.Length <= 21))
            {
                //string regexPattern = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{1}[0-9]{1}|[0-9])\:\d{1,5}?$";

                string regexPattern = @"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))(\:\d{1,5})?";
                //string regexPattern = @"[a-zA-z]+://(((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))|(localhost))(\:\d{1,5})?/";

                return Regex.IsMatch(sValue, regexPattern, RegexOptions.IgnoreCase);
            }

            return false;
        }

        /// <summary>
        /// 判断是否是IP地址格式 1433
        /// </summary>
        /// <param name="sValue">待判断的端口</param>
        /// <returns>true or false</returns>
        public static bool IsPort(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue) && (sValue.Length >= 1 && sValue.Length <= 5))
            {
                // 1到5位的数字，未检查范围
                string regexPattern = @"^\d{1,5}$";

                return Regex.IsMatch(sValue, regexPattern, RegexOptions.IgnoreCase);
            }

            return false;
        }

        #endregion

        #region 替换多余字符串

        /// <summary>
        ///  清除空格函数
        /// </summary>
        /// <param name="strTemp">替换字符串</param>
        /// <returns>返回一个没有空格的字符串</returns>
        public static string ReplaceSpace(string strTemp)
        {
            return Regex.Replace(strTemp, @"\s", "");
        }

        /// <summary>
        /// html空格nbsp;替换为空格(空字符串)
        /// </summary>
        /// <param name="strTemp"></param>
        /// <returns></returns>
        public static string ReplaceHtmlSpace(string strTemp)
        {
            Regex htmlSpaceReg = new Regex("\\&nbsp\\;", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return htmlSpaceReg.Replace(strTemp, "");
        }

        /// <summary>
        /// 把一个以上的空格替换为一个空格(空字符串).
        /// </summary>
        /// <param name="strTemp"></param>
        /// <returns></returns>
        public static string ReplaceSpaceReg(string strTemp)
        {
            Regex spaceReg = new Regex("\\s{2,}|\\ \\;", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return spaceReg.Replace(strTemp, " ");
        }

        #endregion

        #region 字符串替换.仅允许字母、数字、句点(半角)

        /// <summary>
        /// 仅允许字母、数字、句点(半角),其他符号全部替换为指定字符串
        /// @注解:在指定的输入字符串内，使用指定的替换字符串替换与指定正则表达式匹配的所有字符串。
        /// @注意:用于图片的URL地址
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static string CleanInput_ForImageUrl(string strInput, string replacement)
        {
            if (!string.IsNullOrEmpty(strInput))
            {
                // 仅允许字母、数字、句点(半角),其他符号全部替换为指定字符串

                // Replace invalid characters with empty strings.
                // return Regex.Replace(strIn, @"[^\w\.@-]", "");
                return Regex.Replace(strInput, @"[^A-Za-z0-9\.]", replacement);
            }
            return strInput;
        }

        #endregion

        #region 清除字符串结束符标记

        /// <summary>
        ///  清除字符串结束符标记
        /// </summary>
        /// <param name="strTemp">替换字符串</param>
        /// <returns>返回一个字符串结束符标记的字符串</returns>
        public static string ReplaceEndString(string strTemp)
        {
            // TODO:字符串结束标记.清除
            return Regex.Replace(strTemp, @"\0", "");
        }

        #endregion

        #region 中英文验证

        /// <summary>
        /// 判断是否英文大写
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static bool IsEnglishUpper(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                return Regex.IsMatch(sValue, "^[A-Z]+$");
            }
            return false;
        }

        /// <summary>
        /// 判断是否英文
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static bool IsEnglish(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                return Regex.IsMatch(sValue, "^[A-Za-z]+$");
            }
            return false;
        }

        #endregion

        #region 地址验证

        /// <summary>
        /// 判断网站地址/WebService地址
        /// @支持端口号检查(5位长度的数字)
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static bool IsInternalAddress(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                // @"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"
                // @"[a-zA-z]+://(((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))|(localhost))(\:\d{1,5})?/";

                string regexPattern = @"^http(s)?\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|asmx|[a-zA-Z]{1,10}))(\:[0-9]{1,5})?(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$";

                return Regex.IsMatch(sValue, regexPattern, RegexOptions.IgnoreCase);
            }
            return false;
        }

        #endregion

        #region 电话验证

        /// <summary>
        /// 判断电话号码
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static bool IsTelPhoneValue(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                return Regex.IsMatch(sValue, @"(^(\d{3,4}-)?\d{7,8})$|(13|15|18[0-9]{9})");
            }
            return false;
        }

        #endregion

        #region 邮箱验证

        /// <summary>
        /// 判断邮箱地址
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static bool IsEmail(string sValue)
        {
            if (!string.IsNullOrEmpty(sValue))
            {
                // @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
                string regexPattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9] {1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\)?]$";

                return Regex.IsMatch(sValue, regexPattern, RegexOptions.IgnoreCase);
            }
            return false;
        }

        #endregion

        #region 字符串截取.提取字符串的前缀数字编号


        /// <summary>
        /// 检查指定字符串是否存在，忽略大小写
        /// </summary>
        /// <param name="strInput">要搜索匹配项的字符串</param>
        /// <param name="pattern">要匹配的正则表达式模式</param>
        /// <returns>如果正则表达式找到匹配项，则为 true；否则，为 false。</returns>
        public static bool StringMatch_Regex(string strInput, string pattern)
        {
            if (!string.IsNullOrEmpty(strInput))
            {
                // 任意字符开始或结束，包含中文字符，但不包含英文字符，且中文字符在中间（而非首尾）
                // @"^[\s\S]*[^A-Za-z][\u4e00-\u9fa5]+[\s\S]*$"

                /*
                 * 检查指定字符串是否存在
                 * 表达式 ^[\s\S]*(.log_edittemp|.hztmup_ongoing|.hztmdown_done)+[\s\S]*$
                 * 描述 格式8-4-4-4-12
                 * 匹配的例子 *.log; *.log_edittemp; *.hztmup_ongoing; *.hztmdown_done;
                 */

                if (string.IsNullOrEmpty(pattern))
                {
                    pattern = @".log_edittemp|.hztmup_ongoing|.hztmdown_done"; // 设置默认匹配的关键字
                }

                //if (Regex.Match(strInput, pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
                //{
                //    return true;
                //}

                return Regex.IsMatch(strInput, pattern, RegexOptions.IgnoreCase);
            }

            return false;
        }


        /// <summary>
        /// 在指定的输入字符串内，使用指定的替换字符串替换与指定正则表达式匹配的所有字符串。
        /// </summary>
        /// <param name="strInput"></param>
        /// <param name="regexPattern"></param>
        /// <param name="replacement"></param>
        /// <returns></returns>
        public static string SubString_WithRegex(string strInput, string regexPattern, string replacement)
        {
            if (!string.IsNullOrEmpty(strInput))
            {
                // 仅允许字母、数字、句点(半角),其他符号全部替换为指定字符串

                // Replace invalid characters with empty strings.
                //if (string.IsNullOrEmpty(regexPattern)) { regexPattern = @"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/"; }
                //if (string.IsNullOrEmpty(replacement)) { replacement = "${proto}${port}"; }
                if (string.IsNullOrEmpty(regexPattern)) { regexPattern = @"^(?<numericNo>\d+)(\D*)"; }  // 以数字开头的任意字符串,长度不限制,末尾字符不限制
                if (string.IsNullOrEmpty(replacement)) { replacement = "${numericNo}"; }                // 要使用的替换模式

                Regex r = new Regex(regexPattern, RegexOptions.None);
                return r.Match(strInput).Result(replacement);
            }
            return strInput;
        }

        /// <summary>
        /// 在指定的输入字符串内，使用指定的替换字符串替换与指定正则表达式匹配的所有字符串。
        /// </summary>
        /// <param name="strInput"></param>
        /// <param name="regexPattern"></param>
        /// <returns></returns>
        public static bool CheckString_WithRegex(string strInput, string regexPattern)
        {
            if (!string.IsNullOrEmpty(strInput))
            {
                // 仅允许字母、数字、句点(半角),其他符号全部替换为指定字符串

                // Replace invalid characters with empty strings.
                // return Regex.Replace(strIn, @"[^\w\.@-]", "");
                Regex r = new Regex(regexPattern, RegexOptions.IgnoreCase);
                return r.Match(strInput).Success;
            }

            return false;
        }

        /// <summary>
        /// 提取字符串的前缀数字编号
        /// </summary>
        /// <param name="strInput">输入字符串</param>
        /// <param name="regexPattern">要匹配的正则表达式模式</param>
        /// <param name="replacement">要使用的替换模式</param>
        /// <returns></returns>
        public static string SubString_ForPrefixNumber(string strInput, string regexPattern, string replacement)
        {
            if (!string.IsNullOrEmpty(strInput))
            {
                // 仅允许字母、数字、句点(半角),其他符号全部替换为指定字符串

                // Replace invalid characters with empty strings.
                //if (string.IsNullOrEmpty(regexPattern)) { regexPattern = @"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/"; }
                //if (string.IsNullOrEmpty(replacement)) { replacement = "${proto}${port}"; }
                if (string.IsNullOrEmpty(regexPattern)) { regexPattern = @"^(?<numericNo>\d+)(\D*)"; }  // 以数字开头的任意字符串,长度不限制,末尾字符不限制
                if (string.IsNullOrEmpty(replacement)) { replacement = "${numericNo}"; }                // 要使用的替换模式

                Regex r = new Regex(regexPattern, RegexOptions.None);
                return r.Match(strInput).Result(replacement);
            }
            return strInput;
        }

        #endregion

        #region 解析文件名称

        /// <summary>
        /// 解析文件名称
        /// </summary>
        /// <param name="date_format">文件格式</param>
        /// <param name="dateTime"></param>
        /// <returns>返回文件路径</returns>
        public static string GetDataFloder(string date_format, DateTime detectDate)
        {
            string strPath = detectDate.ToString(date_format, DateTimeFormatInfo.InvariantInfo);
            return strPath;
        }

        #endregion
    }
 
